package com.buho29.rcmanager;


import com.buho29.rcmanager.utils.Format;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Vibrator;
import android.preference.PreferenceManager;
import android.util.Log;

public class AlarmReceiver extends BroadcastReceiver {

	public static final String ACTION_ALARM = "com.buho29.rcmanager.ACTION_ALARM";
	
	@Override
	public void onReceive(Context context, Intent intent) {
	  
		CharSequence title = context.getText(R.string.app_name);
		String text = context.getString(R.string.notification_chrono); 
			
		
		if(ChronoView.ALARM_SG>1){
			Log.e("AlarmReceiver", "alarm is "+ChronoView.ALARM_SG);
			text+=" "+Format.formatSecondsToString(ChronoView.ALARM_SG);
		}
		
		NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        
        
        
    	Intent i = new Intent(context, ChronoView.class);
    	
        // This is who should be launched if the user selects our notification.
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
               i , PendingIntent.FLAG_UPDATE_CURRENT);

        final Notification notification = new Notification( 
        		R.drawable.icon,       // the icon for the status bar
                text,System.currentTimeMillis()); // the timestamp for the notification

        notification.setLatestEventInfo(context,title,text,contentIntent);

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        
        if(prefs.getBoolean("vibrator_enabled", false)){
        	Vibrator vibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
        	
        	
        	String[] ps = prefs.getString("vibrator_pattern", "").split(" ");
        	
			try {
				long[] pattern ={100,0,100,0,200};
				if(ps.length>0){
					pattern = new long[ps.length];
					for (int j = 0; j < ps.length; j++) {
						pattern[j] = Long.parseLong(ps[j]);
					}	
				}
				
				vibrator.vibrate(pattern,-1);
			} catch (NumberFormatException e) {
				Log.e("AlarmReceiver", e+"");
			}
        	
        }
        
        if(prefs.getBoolean("sound_enabled", false)){
        	String uri = String.valueOf(android.provider.Settings.System.DEFAULT_NOTIFICATION_URI); 
        	String r = prefs.getString("ringtone_uri",uri);  
        
        	try {
        		MediaPlayer mp = MediaPlayer.create(context, Uri.parse(r));
				mp.start();
			} catch (Exception e) {
				Log.e("AlarmReceiver", e+"");
			} 
        }
        
        mNotificationManager.notify( R.layout.chrono, notification);   // notification
        
	}
  
}
